#include "Capture.h"
#include "concurrent/ThreadManager.h"

#include "AndroidAudioCapture.h"
#include "AndroidCameraCapture.h"
#include "MP4Capture.h"
#include "RTMPCapture.h"
#include "RTSPCapture.h"

void ICapture::handleMessage(Message *pMsg) {
  switch (pMsg->what) {
    case MsgID_Init:
      break;
    case MsgIDCapture_Start:
      startCapture();
      break;
    case MsgIDCapture_Stop:
      stopCapture();
      break;
  }
}

void ICapture::toString() {
  if (_pDevice) {
    _pDevice->toString();
  }
}

void ICapture::registerFrameCallback(IFrameCallback *pFrameCallback) {
  _pFrameCallback = pFrameCallback;
}

void ICapture::unregisterFrameCallback() {
  _pFrameCallback = nullptr;
}

ICapture *Capture_Create(IDevice *pDevice) {
  ICapture *pCapture = nullptr;
  if (pDevice) {
    switch (pDevice->devType) {
      case DeviceType_Audio:
        pCapture = new AndroidAudioCapture(pDevice);
        break;
      case DeviceType_Camera:
        pCapture = new AndroidCameraCapture((CameraDevice *)pDevice);
        break;
      case DeviceType_MP4:
        pCapture = new MP4Capture((IPCameraDevice *) pDevice);
        break;
      case DeviceType_RTMP:
        pCapture = new RTMPCapture((IPCameraDevice *) pDevice);
        break;
      case DeviceType_RTSP:
        pCapture = new RTSPCapture((IPCameraDevice *) pDevice);
        break;
    }

    if (pCapture) {
      ThreadManager::Instance()->createThread(pCapture);
    }
  }

  return pCapture;
}

void Capture_Destroy(ICapture *pCapture) {
  if (pCapture && pCapture->isAlive()) {
    ThreadManager::Instance()->destroyThread(pCapture);
  }
}

bool Capture_Start(ICapture *pCapture) {
  if (pCapture && pCapture->isAlive()) {
    return pCapture->sendMessage(MsgIDCapture_Start);
  }

  return false;
}

void Capture_Stop(ICapture *pCapture) {
  if (pCapture && pCapture->isAlive()) {
    pCapture->sendMessage(MsgIDCapture_Stop);
  }
}
